//
//  Dictionary+JJExtension.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/26.
//

import Foundation

public extension Dictionary {
    /// 字典转JSON string
    ///
    /// - Parameters:
    ///   - prettyPrint: 是否prettyPrint
    /// - Returns: JSON string
    func toJSONString(prettyPrint: Bool = false) -> String? {
        if JSONSerialization.isValidJSONObject(self),
           let jsonData = try? JSONSerialization.data(withJSONObject: self, options: prettyPrint ? .prettyPrinted : []) {
            return String(data: jsonData, encoding: .utf8)
        }
        JJLog("\(self)) is not a valid JSON Object")
        return nil
    }
}
